package com.caiden_micheal.user.service.impl;

import com.alibaba.fastjson2.JSON;
import com.caiden_micheal.base.constant.LoggerHead;
import com.caiden_micheal.base.properties.AESProperties;
import com.caiden_micheal.base.properties.URLProperties;
import com.caiden_micheal.base.utils.AESUtil;
import com.caiden_micheal.user.utils.EmailSenderUtil;
import com.caiden_micheal.base.utils.EncodeURIComponent;
import com.caiden_micheal.base.utils.GenerateCodeUtil;
import com.caiden_micheal.user.domain.User;
import com.caiden_micheal.user.service.EmailSendService;
import com.caiden_micheal.user.vo.RegisterVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.HashMap;
import java.util.Map;

@Service
public class EmailSendServiceImpl implements EmailSendService {
    private static final Logger logger = LoggerFactory.getLogger(EmailSendServiceImpl.class);
    @Autowired
    private URLProperties urlProperties;
    @Autowired
    private EmailSenderUtil emailSenderUtil;
    @Autowired
    private AESProperties aesProperties;

    @Override
    public Boolean sendRegisterEmail(User user, RegisterVO registerVO) {
        String to = null, emailSubject = null, emailContent = null;

        if (registerVO != null) {
            to = registerVO.getEmail();
            emailSubject = "新用户激活确认";
            Map<String, String> encrypt = new HashMap<>();
            encrypt.put("userId", String.valueOf(registerVO.getId()));
            encrypt.put("time", String.valueOf(System.currentTimeMillis()));
            String content = AESUtil.encrypt(JSON.toJSONString(encrypt), aesProperties.getSecretKey());
            String url = urlProperties.getBackBaseUrl() + "/api/user/activate?data=" + EncodeURIComponent.encode(content, "UTF-8");
            emailContent = "<!DOCTYPE html><html><head><meta charset='utf-8'><meta name='viewport'content='width=device-width, initial-scale=1'><title>用户激活</title></head><body><div class='div1'><div class='div2'>用户激活确认</div><div class='div3'><div class='div4'>尊敬的用户：</div><div class='div5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;此邮件是注册的最后一步，请点击下方链接完成注册。注意：若非本人操作，请勿点击链接。</div><div class='div6'><a href='" + url + "'target='_blank'>" + url + "</a></div><div class='div7'>提醒：当前注册的用户名为" + registerVO.getUsername() + "<br>链接有效时间为<span style='margin: 0px 5px 0px 5px;'>5分钟</span>，时间过后视为注册失败，请及时确认并点击链接完成注册。</div></div><div style='margin-left: 150px;margin-top: 30px;'><img style='margin-bottom: 20px;' width='150px' src='" + urlProperties.getMailImgLogoUrl() + "'/></div></div></body><style type='text/css'>.div1{width:450px;height:500px;border:2px solid#192928;margin:10px 0 0 10px;border-radius:15px}.div2{text-align:center;margin-top:10px;font-size:20px}.div3{margin:5px 10px 0px 10px}.div5{margin-top:10px;line-height:30px;color:#192928}.div6{margin-top:20px}.div7{margin-top:20px;line-height:25px}</style></html>";
        } else if (user != null) {
            to = user.getEmail();
            emailSubject = "密码修改邮箱验证";
            String emailCode = GenerateCodeUtil.generateRandomString(12);
            emailContent = "<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content=\"width=device-width,initial-scale=1\"><title>密码修改邮箱验证</title><body><div class=div1><div class=div2>密码修改邮箱验证</div><div class=div3><div class=div4>尊敬的用户：</div><div class=div5>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;此邮件是修改密码的重要步骤之一，请将下方验证码填入密码修改界面的\"邮箱验证码\"输入框。注意：若非本人操作，请勿泄露该验证码。</div><div class=div6>" + emailCode + "</div><div class=div7>验证码有效时间为<span style=\"margin:0 5px 0 5px\">5分钟</span>，时间过后视为放弃密码修改。</div></div><div style=margin-left:150px;margin-top:30px><img width=150px src=\"https://www.peugeot-motocycles.com.cn/application/portal/view/public/images/footer_logo.png\"/></div></div><style>.div1{width:450px;height:500px;border:2px solid#192928;margin:10px 0 0 10px;border-radius:15px}.div2{text-align:center;margin-top:10px;font-size:20px}.div3{margin:5px 10px 0 10px}.div5{margin-top:10px;line-height:30px;color:#192928}.div6{text-align:center;margin-top:20px;color:#872121;font-size:20px;font-weight:700}.div7{margin-top:20px;line-height:25px}</style>";
        }
        try {
            emailSenderUtil.sendEmail(to, emailSubject, emailContent);
        } catch (MessagingException e) {
            logger.error(LoggerHead.errorHead() + "Email sending exception, " + e.getMessage());
            return false;
        }
        return true;
    }
}
